package JaggSoft.Utilidades;

import java.awt.event.*;
import javax.swing.*;

public class ManejadorEventoClick implements ActionListener {
    private JDesktopPane panel;
    private String clase;
    public ManejadorEventoClick(JDesktopPane p,String c) {
        this.panel=p;
        this.clase=c;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        for(JInternalFrame j:panel.getAllFrames()){
            if(j.getClass().getName().equals(clase)){
                try { j.setSelected(true); } catch (Exception ex) {}
                if(JOptionPane.showConfirmDialog(null,"Desea cerrar la ventana de "+j.getTitle()+"?","Cerrar Ventana",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
                    j.dispose();
                    try {
                        JInternalFrame nuevo=(JInternalFrame)Class.forName(clase).newInstance();
                        panel.add(nuevo);
                        nuevo.show();
                        return;
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(null,"Error al crear una nueva ventana","JaggSoft",JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                else return;
            }
        }
        try {
            JInternalFrame nuevo=(JInternalFrame)Class.forName(clase).newInstance();
            panel.add(nuevo);
            nuevo.show();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,"Error al crear una nueva ventana","JaggSoft",JOptionPane.INFORMATION_MESSAGE);
        }
    }
}